package com.itany.interceptor;

import com.itany.ApplicationCotextHold;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * serivce cas
 *
 * TGT --> token
 *
 * ST --> 一对多
 *
 */
public class MyInterceptor implements HandlerInterceptor {


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {


    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
         StringRedisTemplate redisTemplate = ApplicationCotextHold.getBean(StringRedisTemplate.class);
         boolean flag = false;
         Cookie[] cookies = request.getCookies();
         if(null!=cookies){
            for(int i=0;i<cookies.length;i++){
                Cookie ck = cookies[i];
                if("TT_TOKEN".equals(ck.getName())){
                    String token = ck.getValue();
                    String str = redisTemplate.opsForValue().get("USER_TOKEN::"+token);
                    System.out.println("------------"+str+"--------------");
                    flag=true;
                }

            }
         }
         return flag;
    }
}
